package com.demo.java.concurrent.threadpool;

import com.demo.java.utils.LogUtils;
import com.demo.java.utils.ThreadUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolExample {

    @Test
    public void bitTest() {
        System.out.println(Integer.toBinaryString(2^31));            // 0000 0000 0000 0000 0000 0000 0001 1101
        System.out.println(Integer.toBinaryString(2147483647));      // 0111 1111 1111 1111 1111 1111 1111 1111
        System.out.println(Integer.toBinaryString(1 << 29));         // 0010 0000 0000 0000 0000 0000 0000 0000
        System.out.println(Integer.toBinaryString(-1));              // 1111 1111 1111 1111 1111 1111 1111 1111
        System.out.println("=============================");
        System.out.println(Integer.toBinaryString(-1 << 29));        // 1110 0000 0000 0000 0000 0000 0000 0000   RUNNING
        System.out.println(Integer.toBinaryString(0 << 29));         // 0000 0000 0000 0000 0000 0000 0000 0000   SHUTDOWN
        System.out.println(Integer.toBinaryString(1 << 29));         // 0001 0000 0000 0000 0000 0000 0000 0000   STOP
        System.out.println(Integer.toBinaryString(2 << 29));         // 0010 0000 0000 0000 0000 0000 0000 0000   TIDYING
        System.out.println(Integer.toBinaryString(3 << 29));         // 0110 0000 0000 0000 0000 0000 0000 0000   TERMINATED
        System.out.println(Integer.toBinaryString((1 << 29) - 1));   // 0001 1111 1111 1111 1111 1111 1111 1111   CAPACITY
    }

    @Test
    public void threadPoolTest1() {
        String tag = "test";
        ThreadFactory tf = new ThreadFactory() {
            AtomicInteger seq = new AtomicInteger(1);
            @Override
            public Thread newThread(@NotNull Runnable r) {
                return new Thread("apppool-" + seq.getAndIncrement() + "-" + tag);
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                1,
                2,
                Integer.MAX_VALUE,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2),// 执行第五个线程抛异常,
//                new LinkedBlockingQueue<>(10),
                tf);
        for (int i = 0; i < 6; i++) { // 执行第五个线程抛异常,
            System.out.println(i);
            executor.execute(() -> {
                System.out.println(LogUtils.log("thread start"));
                ThreadUtils.sleep(Integer.MAX_VALUE);
            });
        }

        ThreadUtils.sleep(Integer.MAX_VALUE);
    }

    @Test
    public void cachedThreadPoolTest() {
        ExecutorService splitExportExecutor = Executors.newCachedThreadPool();
        splitExportExecutor.execute(() -> {
            System.out.println("run");
        });
        ThreadUtils.sleep(2);
        splitExportExecutor.shutdown();
    }

    public static void threadPoolTest() {
        AtomicInteger counter = new AtomicInteger(0);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 2,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(10),
                new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 16; i++) {
            executor.execute(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"-"+counter.incrementAndGet());
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        System.out.println(Thread.currentThread().getName());
    }
}
